package com.feihao.zhou.controller;

import com.feihao.zhou.bean.User;
import com.feihao.zhou.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {

    static UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String username = req.getParameter("username");
        String password = req.getParameter("password");

        System.out.println(username + "," + password);
        //封装user对象：
        User user = new User(username, password);

        User user1 = userService.checkOutUser(user);

        if (user1 != null) {
            //登录成功：将用户名设置到会话域：
            req.getSession().setAttribute("username", username);

            resp.getWriter().write("登录成功！，2秒跳转");
            resp.setHeader("Refresh", "2;URL=/show.jsp");
        } else {
            resp.getWriter().write("登录失败！，请检查用户名或密码！！2秒跳转登录页面");
            resp.setHeader("Refresh", "2;URL=/login.jsp");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
